package gases.as3.librarys.compnents.ui.control.group
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	import gases.as3.librarys.compnents.events.GButtonEvent;
	import gases.as3.librarys.compnents.ui.control.GButton;
	import gases.as3.librarys.core.Interface.IDispose;

	public class GButtonGroup extends EventDispatcher implements IDispose
	{
		public function GButtonGroup(selectNum:uint)
		{
			_selectNum = selectNum;
			super();
		}
		
		protected function initialization():void
		{
			_btns = new Array();
			_btnDic = new Dictionary();
		}
		
		/**
		 * 增加一个ID 
		 * @param btn
		 * 
		 */		
		public function addButton(btn:GButton):void
		{
			_btns.push(btn.id);
			_btnDic[btn.id] = btn;
			setButtonProperty(btn);
		}
		
		/**
		 * 删除按钮 
		 * @param id
		 * 
		 */		
		public function deleteButton(id:String):void
		{
			var index:int = _btns.indexOf(id);
			if(index != -1)
			{
				_btns.splice(index, 1);
			}
			if(_btnDic[id])
			{
				_btnDic[id] = null;
				delete _btnDic[id];
			}
		}
		
		/**
		 * 选中的按钮
		 * 
		 */		
		public function set selectBtns(value:Array):void
		{
			_selectBtns = value.concat();
			changeSelectBtnsState();
		}
		
		/**
		 * 选中的按钮 
		 * @return 
		 * 
		 */		
		public function get selectBtns():Array
		{
			return _selectBtns.concat();
		}
		
		/**
		 * 设置可选数量 
		 * @param value
		 * 
		 */		
		public function set selectNum(value:uint):void
		{
			_selectNum = value;
		}
		
		/**
		 * 获取可选数量 
		 * 
		 */		
		public function get selectNum():void
		{
			return _selectNum;
		}
		
		/**
		 * 获取按钮数量 
		 * @return 
		 * 
		 */		
		public function get btnNum():int
		{
			return _btns.length;
		}
		
		/**
		 * 改变选中按钮状态 
		 * 
		 */		
		protected function changeSelectBtnsState():void
		{
			
		}
		
		private function setButtonProperty(btn:GButton):void
		{
			btn.isSelect = true;
			btn.addEventListener(GButtonEvent.BUTTON_SELECT, onButtonSelect);
			btn.addEventListener(GButtonEvent.BUTTON_UNSELECT, onButtonSelect);
		}
		
		private function removeButtonPorperty(btn:GButton):void
		{
			btn.isSelect = false;
			btn.addEventListener(GButtonEvent.BUTTON_SELECT, onButtonSelect);
			btn.addEventListener(GButtonEvent.BUTTON_UNSELECT, onButtonSelect);
		}
		
		public function dispose():void
		{
			for each(var btn:GButton in _btnDic)
			{
				removeButtonPorperty(btn);	
			}
			_btnDic = null;
			_btns = null;
			_selectBtns = null;
			super.dispose();
		}
		
		private var _btns:Array;
		private var _btnNum:uint = 0;
		private var _selectNum:uint = 0;
		private var _selectBtns:Array = [];
		private var _btnDic:Dictionary;
	}
}



